package src.billing.subsystem;

import java.net.*;
import java.io.*;

import src.constants.ResellerConstants;

public class BillingSystem {

    //public void billingService() throws Exception {
    public BillingSystem() throws Exception {
        String query;
        String customerInfo;
        String customerId;

        String clientRequest;
        String returnedQuery;

        ServerSocket welcomeSocket = new ServerSocket(ResellerConstants.INT_BILLING_PORT);

        while(true) {
            Socket connectionSocket = welcomeSocket.accept();

            BufferedReader inFromClient =
                                new BufferedReader(new
                                InputStreamReader(connectionSocket.getInputStream()));

            DataOutputStream  outToClient =
                                new DataOutputStream(connectionSocket.getOutputStream());

            // THIS is the line that contains the request
            clientRequest = inFromClient.readLine();

            /*****************************************************************************/
            /* System Implementation                                                     */
            /*****************************************************************************/

            Bill billObject = new Bill(clientRequest);

            query = queryVerification(billObject.getQueryName());

            if(query.equals("CUSTOMER_NUMBER_LOOKUP")) {
                customerInfo = queryInformation(billObject);
                if(!customerInfo.equals("INSUFFICIENT_PARAMETERS")) {
                    customerId = idSearch(customerInfo);
                    if(!customerId.equals("CUSTOMER_NOT_FOUND")) {
                        returnedQuery = "|OK|" + billObject.getQueryName() + "|" + customerId + "|";
                    }
                    else {
                        returnedQuery = "|OK|" + billObject.getQueryName() + "|";
                    }
                }
                else {
                    returnedQuery = "|ERR|" + billObject.getQueryName() + "|Insufficient parameters|";
                }
            }
            else {
                /* undefined request */
                returnedQuery = null;
            }

            outToClient.writeBytes(returnedQuery + "\n");
            
            outToClient.flush();
        }
    }

    /**
     * Must be private as the only external interface for this class is via sockets.
     * @param queryName
     * @return
     */
    private static String queryVerification(String queryName) {
        if(queryName.equals("CNX12")) {
            return("CUSTOMER_NUMBER_LOOKUP");
        }
        else {
            return("REQUEST_NOT_FOUND");
        }
    }

    /**
     * Must be private as the only external interface for this class is via sockets.
     * @param billObject
     * @return
     */
    private static String queryInformation(Bill billObject) {
        if(billObject.getParamLength() != 7) {
            return("INSUFFICIENT_PARAMETERS");
        }
        else {
            return("|" + billObject.getParamN(1) +
                    "|" + billObject.getParamN(2) +
                    "|" + billObject.getParamN(3) +
                    "|" + billObject.getParamN(4) +
                    "|" + billObject.getParamN(5) +
                    "|" + billObject.getParamN(6) +
                    "|");
        }
    }

    /*
     * Must be private as the only external interface for this class is via sockets.
     */
    private  static String idSearch(String customerInfo) {
        if(customerInfo.equals("|Fred Flintstone|3800 Finnerty Road|Victoria|BC|V8P 5C2|Canada|")) {
            return("123456789");
        }
        else {
            return ("CUSTOMER_NOT_FOUND");
        }
    }

}
